<template>
  <div class="my-page">
    <div class="pic">
      <img src="../assets/bg.png" alt="" />
      <div class="myportrait">
        <div class="Myavata">
          <img src="../assets/avatar.png" alt="" />
          <p>{{ uname }}</p>
          <van-button type="primary" @click="btn">{{ btncontent }}</van-button>
          <div class="edit" v-show="flag">编辑个人资料</div>
        </div>
      </div>
    </div>

    <van-grid :border="false" :column-num="3">
      <van-grid-item
        icon="star-o"
        text="我的收藏"
        @click="$router.push('/collection')"
      />
      <van-grid-item icon="wap-home-o" text="我的出租" />
      <van-grid-item icon="clock-o" text="看房记录" />
      <van-grid-item icon="idcard" text="成为房主" />
      <van-grid-item icon="manager-o" text="个人资料" />
      <van-grid-item icon="service-o" text="联系我们" />
    </van-grid>
    <div class="advert"><img src="../assets/join.png" alt="" /></div>
  </div>
</template>

<script>
import request from "@/utils/request";
import { Dialog } from "vant";
// import router from "@/router";
export default {
  name: "my-page",
  data() {
    return {
      username: "",
      password: "",
      uname: "游客",
      btncontent: localStorage.getItem("token") ? "退出" : "去登录",
      flag: localStorage.getItem("token"),
    };
  },
  async created() {
    const data = await request({ url: "/user" });
    localStorage.getItem("token") && (this.uname = data.body.nickname);
  },
  methods: {
    btn() {
      if (this.btncontent === "退出") {
        Dialog.confirm({
          title: "确定退出登录吗",
          message: "是否要退出登录",
        })
          .then(() => {
            // on confirm

            localStorage.removeItem("token");
            this.uname = "游客";
            this.btncontent = "去登录";
            this.flag = null;
          })
          .catch(() => {
            // on cancel
          });
      } else {
        this.$router.push("/login");
      }
    },
  },
};
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}
.pic {
  position: relative;
  img {
    height: 190px;
    width: 100%;
    vertical-align: top;
  }

  .myportrait {
    position: absolute;
    transform: translate(-50%, -50%);
    left: 50%;
    top: 100%;
    box-shadow: 0 0 10px 3px #ddd;
    width: 280px;
    height: 165px;
    background-color: #fff;
    margin: 30px 0;
    padding: 0 20px;
    .Myavata {
      img {
        box-shadow: 0 2px 2px #bdbdbd;
        border-radius: 50%;
        border: 5px solid #f5f5f5;
        position: absolute;
        width: 60px;
        height: 60px;
        left: 50%;
        transform: translate(-50%, -50%);
      }
      p {
        text-align: center;
        position: absolute;
        width: 100%;
        left: 50%;
        transform: translate(-50%, -50%);
        top: 40%;
        font-size: 13px;
        // margin-bottom: 10px;
      }
      .van-button {
        position: absolute;
        transform: translate(-50%, -50%);
        top: 65%;
        border-radius: 5px;
        left: 50%;
        background-color: #21b97a;
        font-size: 13px;
        height: 30px;
        line-height: 30px;
        padding: 0 15px;
      }
    }
  }
}
.edit {
  position: absolute;
  transform: translate(-50%, -50%);
  top: 85%;

  left: 50%;
  color: #999 !important;
  font-size: 12px;
}
::v-deep .van-grid {
  margin: 105px 0 0 0;
  .van-grid-item {
    padding-top: 15px;
  }
  .van-grid-item__text {
    font-size: 13px;
  }
  .van-icon {
    font-size: 20px;
  }
}
.advert {
  width: 100px;
  position: relative;
  img {
    position: absolute;
    width: 345px;
    height: 85px;
    margin-bottom: 100px;
    left: 15%;
    transform: translate(0%, 15%);
  }
}
</style>
